layui.use(['form','layer','laydate','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        table = layui.table;

    //运费列表
    var tableIns = table.render({
        elem: '#freight_list',
        url : '/listFreight',
        cellMinWidth : 95,
        page : false,
        height : "full",
        id : "freight_list_table",
        cols : [[
            {field: 'name', title: '物流名称', align:"center"},
            {field: 'statusStr', title: '是否包邮',align:"center"},
            {field: 'defaultAmount', title: '运费默认金额', align:'center',},
            {title: '操作', width:170, templet:'#freight_list_bar',align:"center"}
        ]]
    });

    //添加运费
    function addFreight(data){
        var index = layui.layer.open({
            title : "添加运费模板",
            type : 2,
            content : "freightAdd.html",
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                if(data){
                    body.find("input[name='name']").val(data.name);
                    body.find("input[name='defaultAmount']").val(data.defaultAmount);
                    body.find("select[name='free']").attr("data-value",data.free);
                    body.find("input[name='freightId']").val(data.id);

                    form.render();
                    form.render("select");
                }
                setTimeout(function(){
                    layui.layer.tips('点击此处返回运费模板列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        });
        layui.layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(index);
        })
    }
    $(".addFreight_btn").click(function(){
        addFreight();
    });

    //列表操作
    table.on('tool(freight_list)', function(obj){
        var layEvent = obj.event,
            data = obj.data;

        if(layEvent === 'edit'){ //编辑
            addFreight(data);
        } else if(layEvent === 'del'){ //删除
            layer.confirm('确定删除此运费模板？',{icon:3, title:'提示信息'},function(index){
                $.get("/deleteFreightById",{
                    id : data.id
                },function(data){
                    tableIns.reload();
                    layer.close(index);
                })
            });
        }
    });
})